﻿using System;

namespace NetTopologySuite.Planargraph
{
	public class Edge : GraphComponent
	{
		public Edge()
		{
		}

		public Edge(DirectedEdge de0, DirectedEdge de1)
		{
			this.SetDirectedEdges(de0, de1);
		}

		public void SetDirectedEdges(DirectedEdge de0, DirectedEdge de1)
		{
			this.dirEdge = new DirectedEdge[]
			{
				de0,
				de1
			};
			de0.Edge = this;
			de1.Edge = this;
			de0.Sym = de1;
			de1.Sym = de0;
			de0.FromNode.AddOutEdge(de0);
			de1.FromNode.AddOutEdge(de1);
		}

		public DirectedEdge GetDirEdge(int i)
		{
			return this.dirEdge[i];
		}

		public DirectedEdge GetDirEdge(Node fromNode)
		{
			if (this.dirEdge[0].FromNode == fromNode)
			{
				return this.dirEdge[0];
			}
			if (this.dirEdge[1].FromNode == fromNode)
			{
				return this.dirEdge[1];
			}
			return null;
		}

		public Node GetOppositeNode(Node node)
		{
			if (this.dirEdge[0].FromNode == node)
			{
				return this.dirEdge[0].ToNode;
			}
			if (this.dirEdge[1].FromNode == node)
			{
				return this.dirEdge[1].ToNode;
			}
			return null;
		}

		internal void Remove()
		{
			this.dirEdge = null;
		}

		public override bool IsRemoved
		{
			get
			{
				return this.dirEdge == null;
			}
		}

		protected DirectedEdge[] dirEdge;
	}
}
